package com.moku.tester;

import java.io.IOException;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

final class TestOptionsScreen extends MainScreen {
	// Cconstants
	// ----------------------------------------------------------------
	private static String DEFAULT_HTTP_URL = "http://m.google.com";
	private static String DEFAULT_INTERVAL = "300000";

	// Members
	// ------------------------------------------------------------------
	private long interval = Long.parseLong(DEFAULT_INTERVAL);
	private String httpUrl = DEFAULT_HTTP_URL;
	private boolean radioTestCheck = true;
	private boolean httpGetTestCheck = false;

	private HttpTester _app;
	private EditField _intervalField;
	private EditField _testUrlField;
	private CheckboxField _radioTestCheckboxField;
	private CheckboxField _httpGetTestCheckboxField;

	private MainScreen _this;
	private MenuItem _save;

	// Constructors
	// -------------------------------------------------------------
	TestOptionsScreen() {
		super();
		_this = this;
		_app = (HttpTester) UiApplication.getUiApplication();

		// Instantiate some cached menu items.
		_save = new MenuItem("Ok", 105, 10) {
			public void run() {
				try {
					_this.save();
				} catch (IOException e) {
					e.printStackTrace();
				}
				_app.popScreen(_this);
			}
		};

		setTitle(new LabelField("Tester Options", LabelField.ELLIPSIS
				| LabelField.USE_ALL_WIDTH));
		LabelField label1 = new LabelField("Parameter");
		label1.setFont(Font.getDefault().derive(Font.BOLD));
		add(label1);
		add(new SeparatorField());
		_intervalField = new EditField("Interval: ", DEFAULT_INTERVAL,
				EditField.DEFAULT_MAXCHARS, EditField.FILTER_INTEGER);
		_testUrlField = new EditField("Http URL: ", DEFAULT_HTTP_URL);
		add(_intervalField);
		add(_testUrlField);

		LabelField label2 = new LabelField("TestSuite");
		label2.setFont(Font.getDefault().derive(Font.BOLD));
		add(label2);
		add(new SeparatorField());
		_radioTestCheckboxField = new CheckboxField("Radio",
				this.radioTestCheck);
		_httpGetTestCheckboxField = new CheckboxField("Http GET",
				this.httpGetTestCheck);

		add(_radioTestCheckboxField);
		add(_httpGetTestCheckboxField);
		addMenuItem(_save);
	}

	// Methods
	// ------------------------------------------------------------------
	void display() {
		// _radioTestCheckboxField.setChecked(this.radioTestCheck);
		// _httpGetTestCheckboxField.setChecked(this.httpGetTestCheck);
		_app.pushScreen(this);
	}

	public long getInterval() {
		return interval;
	}

	public String getHttpUrl() {
		return httpUrl;
	}

	public boolean isRadioTestCheck() {
		return radioTestCheck;
	}

	public boolean isHttpGetTestCheck() {
		return httpGetTestCheck;
	}

	/**
	 * Called when there have been changes and users saves screen.
	 */
	public void save() {
		this.httpUrl = _testUrlField.getText();
		this.interval = Long.parseLong(_intervalField.getText());
		this.radioTestCheck = _radioTestCheckboxField.getChecked();
		this.httpGetTestCheck = _httpGetTestCheckboxField.getChecked();
	}

}
